﻿using CuteSprite.Eme.Data.Settlement;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components;
using HmFramework.Components.Controls;
using HmFramework.Components.Editors;
using HmFramework.Data;
using HmFramework.UI.Utils.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CuteSprite.Eme.UI.Settlement
{
  public partial class FrmAdditionalCost : EmeEntityForm
  {
    #region -- 构造 --

    #region - 新增 -

    /// <summary>默认构造函数</summary>
    public FrmAdditionalCost(String settlementSolutionCode, IEntity entity)
      : base(entity, false)
    {
      InitializeComponent();
      InitForm();
    }

    #endregion

    #region - 编辑或新增 -

    /// <summary>构造函数</summary>
    /// <param name="entity">实体对象</param>
    /// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
    public FrmAdditionalCost(IEntity entity, Boolean readOnly)
      : base(entity, readOnly)
    {
      InitializeComponent();
      InitForm();
    }

    #endregion

    #endregion

    #region -- 窗体事件 --

    #region - method InitComponent -

    ///// <summary>控件初始化</summary>
    protected override void InitComponent()
    {
      base.InitComponent();

      if (frmBusinessCategory == null) { frmBusinessCategory = FindControl("frmBusinessCategory") as ComboBoxEx; }
      if (frmGoodsCategory == null) { frmGoodsCategory = FindControl("frmGoodsCategory") as ComboBoxEx; }

      #region 燃油计费模式,燃油计费数值

      if (frmFuelModel == null) { frmFuelModel = FindControl("frmFuelModel") as ComboBoxEx; }
      if (frmFuelData == null) { frmFuelData = FindControl("frmFuelData") as NumberInput; }
      if (frmFuelData2 == null) { frmFuelData2 = FindControl("frmFuelData2") as IntegerInput; }
      if (frmFuelModel != null)
      {
        frmFuelModel.SelectedIndexChanged += new EventHandler((sender, e) =>
        {
          var expressType = (ExpressSettlementFuelModel)frmFuelModel.SelectedValue;
          switch (expressType)
          {
            case ExpressSettlementFuelModel.IncludedFuelCosts:
              frmFuelData2.Visible = false;
              frmFuelData.Visible = true;
              frmFuelData.Enabled = false;
              frmFuelData.Text = "0";
              frmFuelData.Value = 0;
              break;

            case ExpressSettlementFuelModel.PercentageTotalBilling:
            case ExpressSettlementFuelModel.DiscountPercentageBilling:
              frmFuelData2.Visible = true;
              frmFuelData.Visible = false;
              frmFuelData.Enabled = true;
              break;

            case ExpressSettlementFuelModel.Cash:
              frmFuelData2.Visible = false;
              frmFuelData.Visible = true;
              frmFuelData.Enabled = true;
              frmFuelData.Value = 0;
              break;

            default:
              frmFuelData2.Visible = false;
              frmFuelData.Visible = true;
              frmFuelData.Enabled = false;
              frmFuelData.Value = 0;
              frmFuelData.Text = "0";
              break;
          }
        });
      }

      #endregion

      #region 是否包括报关费用,过港车费，临时仓储，其它费用
      //报关费用
      if (frmIsCustomsDeclaration == null) { frmIsCustomsDeclaration = FindControl("frmIsCustomsDeclaration") as CheckBoxX; }
      if (frmCustomsDeclarationCost == null) { frmCustomsDeclarationCost = FindControl("frmCustomsDeclarationCost") as NumberInput; }

      if (frmIsCustomsDeclaration != null)
      {
        frmIsCustomsDeclaration.CheckedChanged += new EventHandler((sender, e) =>
          {
            frmCustomsDeclarationCost.Enabled = frmIsCustomsDeclaration.Checked == false;
          });
      }
      //过港车费
      if (frmIsFareCustomsHK == null) { frmIsFareCustomsHK = FindControl("frmIsFareCustomsHK") as CheckBoxX; }
      if (frmIsFareCustomsHK != null)
      {
        frmIsFareCustomsHK.CheckedChanged += new EventHandler((sender, e) =>
        {
          frmFareCustomsHK.Enabled = frmIsFareCustomsHK.Checked == false;
        });
      }
      //临时仓储
      if (frmIsWarehousingTemporary == null) { frmIsWarehousingTemporary = FindControl("frmIsWarehousingTemporary") as CheckBoxX; }
      if (frmIsWarehousingTemporary != null)
      {
        frmIsWarehousingTemporary.CheckedChanged += new EventHandler((sender, e) =>
        {
          frmWarehousingTemporary.Enabled = frmIsWarehousingTemporary.Checked == false;
        });
      }
      //其它费用
      if (frmIsOtherCost == null) { frmIsOtherCost = FindControl("frmIsOtherCost") as CheckBoxX; }
      if (frmIsOtherCost != null)
      {
        frmIsOtherCost.CheckedChanged += new EventHandler((sender, e) =>
        {
          frmOtherCost.Enabled = frmIsOtherCost.Checked == false;
        });
      }

      #endregion

      this.OnSaving += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmSettlementSolution_OnSaving);
      this.OnValid += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmBsdSettlementSolution_OnValid);
    }

    #endregion

    #region - method FormOnPreLoad -

    protected override void FormOnPreLoad()
    {
      base.FormOnPreLoad();

      if (IsAddNew)
      {
        frmCustomsDeclarationCost.Value = 0;
        frmCustomsDeclarationCost.ValueObject = 0;
        frmFareCustomsHK.Value = 0;
        frmFareCustomsHK.ValueObject = 0;
        frmWarehousingTemporary.Value = 0;
        frmWarehousingTemporary.ValueObject = 0;
        frmOtherCost.Value = 0;
        frmOtherCost.ValueObject = 0;
      }
    }

    #endregion

    #region - method FormOnLoad -

    public override void FormOnLoad()
    {
      base.FormOnLoad();

      if (!IsAddNew)
      {
        //燃油付费
        if (frmFuelModel != null)
        {
          var expressType = (ExpressSettlementFuelModel)frmFuelModel.SelectedValue;
          switch (expressType)
          {
            case ExpressSettlementFuelModel.IncludedFuelCosts:
              frmFuelData.Value = 0;
              frmFuelData.Text = "0";
              break;

            case ExpressSettlementFuelModel.PercentageTotalBilling:
            case ExpressSettlementFuelModel.DiscountPercentageBilling:
              frmFuelData2.Value = Convert.ToInt32(CurrentEntity[AdditionalCost.__.FuelData]);
              break;

            case ExpressSettlementFuelModel.Cash:
              frmFuelData.Value = Convert.ToDecimal(CurrentEntity[AdditionalCost.__.FuelData]);
              break;

            default:
              frmFuelData.Value = 0;
              frmFuelData.Text = "0";
              break;
          }
        }


      }
    }

    #endregion

    #region - 实体保存 -

    private void FrmSettlementSolution_OnSaving(object sender, EntityFormEventArgs e)
    {
      if (frmFuelModel != null)
      {
        var expressType = (ExpressSettlementFuelModel)frmFuelModel.SelectedValue;
        switch (expressType)
        {
          case ExpressSettlementFuelModel.IncludedFuelCosts:
            CurrentEntity[AdditionalCost.__.FuelData] = 0;
            break;

          case ExpressSettlementFuelModel.PercentageTotalBilling:
          case ExpressSettlementFuelModel.DiscountPercentageBilling:
            CurrentEntity[AdditionalCost.__.FuelData] = frmFuelData2.Value;
            break;

          case ExpressSettlementFuelModel.Cash:
            CurrentEntity[AdditionalCost.__.FuelData] = frmFuelData.Value;
            break;

          default:
            CurrentEntity[AdditionalCost.__.FuelData] = 0;
            break;
        }
      }

      //判断是否包括报关费用,过港车费，临时仓储，其它费用,是则存值为0；
      if (frmIsCustomsDeclaration.Checked != false)
      {
        CurrentEntity[AdditionalCost.__.CustomsDeclarationCost] = 0;
      }
      if (frmIsFareCustomsHK.Checked != false)
      {
        CurrentEntity[AdditionalCost.__.FareCustomsHK] = 0;
      }
      if (frmIsWarehousingTemporary.Checked != false)
      {
        CurrentEntity[AdditionalCost.__.WarehousingTemporary] = 0;
      }
      if (frmIsOtherCost.Checked != false)
      {
        CurrentEntity[AdditionalCost.__.OtherCost] = 0;
      }
    }

    #endregion

    #region - 实体验证 -

    private void FrmBsdSettlementSolution_OnValid(object sender, EntityFormEventArgs e)
    {
      if (frmBusinessCategory != null && (Int32)frmBusinessCategory.SelectedValue <= 0)
      {
        MessageBoxEx.Show("请选择业务类别！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        e.Cancel = true;
        return;
      }
      if (frmGoodsCategory != null && (Int32)frmGoodsCategory.SelectedValue <= 0)
      {
        MessageBoxEx.Show("请选择货物类别！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        e.Cancel = true;
        return;
      }
      if (frmFuelModel != null && (Int32)frmFuelModel.SelectedValue <= 0)
      {
        MessageBoxEx.Show("请选择燃油付费模式！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        e.Cancel = true;
        return;
      }

     
    }

    #endregion

    #endregion
  }
}
